package com.ruoyi.scm.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.response.BaseResult;
import com.ruoyi.scm.domain.ScmPurchasePlan;
import com.ruoyi.scm.service.IScmPurchasePlanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 采购计划Controller
 *
 * @author Ryan
 * @date 2025-03-12
 */
@Api(tags="供应链SCM-采购管理-采购计划")
@RestController
@RequestMapping("/scm/purchase/plan")
public class ScmPurchasePlanController extends BaseController
{
    @Autowired
    private IScmPurchasePlanService scmPurchasePlanService;

    /**
     * 查询采购计划列表
     */
    @ApiOperation("采购管理-采购计划列表")
    @GetMapping("/list")
    public TableDataInfo<List<ScmPurchasePlan>> getList(ScmPurchasePlan scmPurchasePlan)
    {
        startPage();
        List<ScmPurchasePlan> list = scmPurchasePlanService.getList(scmPurchasePlan);
        return getDataTable(list);
    }

    /**
     * 导出采购计划列表
     */
    @ApiOperation("导出采购管理-采购计划列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, ScmPurchasePlan scmPurchasePlan)
    {
        List<ScmPurchasePlan> list = scmPurchasePlanService.getList(scmPurchasePlan);
        ExcelUtil<ScmPurchasePlan> util = new ExcelUtil<ScmPurchasePlan>(ScmPurchasePlan.class);
        util.exportExcel(response, list, "采购计划数据");
    }

    /**
     * 获取采购计划详细信息
     */
    @ApiOperation("采购管理-采购计划详细信息")
    @GetMapping(value = "/{id}")
    public BaseResult<ScmPurchasePlan> getById(@PathVariable("id") Long id)
    {
        return BaseResult.success(scmPurchasePlanService.getById(id));
    }

    /**
     * 新增采购计划
     */
    @ApiOperation("新增采购管理-采购计划")
    @PostMapping
    public BaseResult<Integer> add(@RequestBody ScmPurchasePlan scmPurchasePlan)
    {
        // 申请状态 0未提交 1待审核 2已审核
        scmPurchasePlan.setApplicantStatus(0);
        return BaseResult.success(scmPurchasePlanService.add(scmPurchasePlan));
    }

    /**
     * 修改采购计划
     */
    @ApiOperation("修改采购管理-采购计划")
    @PutMapping
    public BaseResult<Integer> edit(@RequestBody ScmPurchasePlan scmPurchasePlan)
    {
        scmPurchasePlan.setApplicantStatus(0);
        scmPurchasePlan.setApprovedId(null);
        scmPurchasePlan.setApprovedStatus(null);
        scmPurchasePlan.setApprovedDate(null);
        scmPurchasePlan.setApprovedComments("");
        return BaseResult.success(scmPurchasePlanService.edit(scmPurchasePlan));
    }
    @ApiOperation("提交采购计划申请")
    @Log(title = "提交采购计划申请", businessType = BusinessType.UPDATE)
    @PutMapping("/submit")
    public BaseResult<Integer> submit(@RequestBody ScmPurchasePlan scmPurchasePlan) {
        scmPurchasePlan.setApplicantStatus(1);
        scmPurchasePlan.setApplicantDate(DateUtils.getNowDate());
        if(scmPurchasePlan.getId() != null) {
            // 修改提交
            ScmPurchasePlan purchasePlan = scmPurchasePlanService.getById(scmPurchasePlan.getId());
            if(purchasePlan == null) {
                return BaseResult.error("此采购计划不存在，不能提交！");
            }
            scmPurchasePlan.setApprovedId(null);
            scmPurchasePlan.setApprovedStatus(null);
            scmPurchasePlan.setApprovedDate(null);
            scmPurchasePlan.setApprovedComments("");
            return BaseResult.success(scmPurchasePlanService.edit(scmPurchasePlan));
        }else {
            // 新增提交
            return BaseResult.success(scmPurchasePlanService.add(scmPurchasePlan));
        }
    }

    @ApiOperation("审核采购计划申请")
    @Log(title = "审核采购计划申请", businessType = BusinessType.UPDATE)
    @PutMapping("/approve")
    public BaseResult<Integer> approve(@RequestBody ScmPurchasePlan scmPurchasePlan) {
        scmPurchasePlan.setApplicantStatus(2);
        scmPurchasePlan.setApprovedDate(DateUtils.getNowDate());
        scmPurchasePlan.setApprovedId(SecurityUtils.getLoginUser().getUser().getUserId());
        return BaseResult.success(scmPurchasePlanService.edit(scmPurchasePlan));
    }

    /**
     * 删除采购计划
     */
    @ApiOperation("删除采购管理-采购计划")
	@DeleteMapping("/{ids}")
    public BaseResult<Integer> remove(@PathVariable Long[] ids)
    {
        return BaseResult.success(scmPurchasePlanService.del(ids));
    }
}
